package cn.iocoder.yudao.module.system.service.noticeread;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.system.controller.app.noticeread.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.noticeread.NoticeReadDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.noticeread.NoticeReadMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户通知公告阅读记录 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class NoticeReadServiceImpl implements NoticeReadService {

    @Resource
    private NoticeReadMapper noticeReadMapper;

    @Override
    public Long createNoticeRead(AppNoticeReadSaveReqVO createReqVO) {
        // 插入
        NoticeReadDO noticeRead = BeanUtils.toBean(createReqVO, NoticeReadDO.class);
        noticeReadMapper.insert(noticeRead);
        // 返回
        return noticeRead.getId();
    }

    @Override
    public void updateNoticeRead(AppNoticeReadSaveReqVO updateReqVO) {
        // 校验存在
        validateNoticeReadExists(updateReqVO.getId());
        // 更新
        NoticeReadDO updateObj = BeanUtils.toBean(updateReqVO, NoticeReadDO.class);
        noticeReadMapper.updateById(updateObj);
    }

    @Override
    public void deleteNoticeRead(Long id) {
        // 校验存在
        validateNoticeReadExists(id);
        // 删除
        noticeReadMapper.deleteById(id);
    }

    private void validateNoticeReadExists(Long id) {
        if (noticeReadMapper.selectById(id) == null) {
            throw exception(NOTICE_READ_NOT_EXISTS);
        }
    }

    @Override
    public NoticeReadDO getNoticeRead(Long id) {
        return noticeReadMapper.selectById(id);
    }

    @Override
    public List<NoticeReadDO> getNoticeReadByUserIds(Long noticeId, Long userId) {
        final LambdaQueryWrapper<NoticeReadDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(NoticeReadDO::getNoticeId, noticeId)
                .eq(NoticeReadDO::getUserId, userId);
        return noticeReadMapper.selectList(wrapper);
    }

    @Override
    public List<NoticeReadDO> getNoticeReadByUserIds(List<Long> noticeIds, Long userId) {
        final LambdaQueryWrapper<NoticeReadDO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(NoticeReadDO::getNoticeId, noticeIds)
                .eq(NoticeReadDO::getUserId, userId);
        return noticeReadMapper.selectList(wrapper);
    }

    @Override
    public PageResult<NoticeReadDO> getNoticeReadPage(AppNoticeReadPageReqVO pageReqVO) {
        return noticeReadMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer selectUnreadNoticeCountByUserId(Long userId, CommonStatusEnum noticeStatus) {
        return noticeReadMapper.selectUnreadNoticeCountByUserId(userId, noticeStatus.getStatus());
    }

}